import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class GrammarParseInput{
    // 初始化文法规则
        //LexicalMain.DoLex();
        static ArrayList<Formula> formulas = new ArrayList<>();
        static ArrayList<String> nonTerminals = new ArrayList<>();
        static ArrayList<String> terminals = new ArrayList<>();
        //String expandPath = Config.expand;

        static void input(){
        Set<String> nonTerminals1 = new HashSet<>();
        Set<String> terminals1 = new HashSet<>();
        String ExpandString="";
        try{
            ExpandString=readFromTxt(Config.expand);
            System.out.println("------读入文法成功------");
        } catch (Exception e){
            System.out.println("------读入文法失败------");
        }
        String[] everyRowOfExpand=splitContentToChapter(ExpandString);
        for(String eachRowOFExpand : everyRowOfExpand){
            String[] eachStrings=splitContentToChapter1(eachRowOFExpand);
            String[] newArray = new String[eachStrings.length - 2];
            
            System.arraycopy(eachStrings, 2, newArray, 0, eachStrings.length - 2);
            formulas.add(new Formula(eachStrings[0], newArray));
            nonTerminals1.add(eachStrings[0]);
        }
        for(String eachRowOFExpand : everyRowOfExpand){
            String[] eachStrings=splitContentToChapter1(eachRowOFExpand);
            for(int i=2;i<eachStrings.length;i++){
                if(!nonTerminals1.contains(eachStrings[i])){
                    terminals1.add(eachStrings[i]);
                }
            }
            //System.out.println(d[0]);
        }
        //System.out.println(formulas);
        GrammarParseInput.nonTerminals = new ArrayList<>(nonTerminals1);
        GrammarParseInput.terminals = new ArrayList<>(terminals1);
    }


        private static String readFromTxt(String filename) throws Exception {
		Reader reader = null;
		try {
			StringBuffer buf = new StringBuffer();
			char[] chars = new char[1024];
			// InputStream in=new FileInputStream(filename);

			reader = new InputStreamReader(new FileInputStream(filename), "UTF-8");
			int readed = reader.read(chars);
			while (readed != -1) {
				buf.append(chars, 0, readed);
				readed = reader.read(chars);
			}
			return buf.toString();
		} finally {
			close(reader);
		}
        
    }
    private static void close(Closeable inout) {
		if (inout != null) {
			try {
				inout.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
    private static String[] splitContentToChapter(String content) {
		// 提示 使用 content.split(" 第[一,二,三,四,五,六,七,八,九,十,零]{1,5}回 ");正则表达拆分
		// 百度一下正则表达式
		String contents[] = content.split("\\n");
		return contents;
	}
    private static String[] splitContentToChapter1(String content) {
		// 提示 使用 content.split(" 第[一,二,三,四,五,六,七,八,九,十,零]{1,5}回 ");正则表达拆分
		// 百度一下正则表达式
		String contents[] = content.split(" ");
		return contents;
	}
}